<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Client_crid_query {

	function query($_POST) {


		//$this->load->library('client_crid_query');

        $page = isset($_POST['page']) ? $_POST['page'] : 1;
        $rp = isset($_POST['rp']) ? $_POST['rp'] : 10;
        $sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'client_id';
        $sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc';

        $query = isset($_POST['query']) ? $_POST['query'] : false;
        $qtype = isset($_POST['qtype']) ? $_POST['qtype'] : false;

        $name = isset($_POST['name']) ? $_POST['name'] : false;
        $recommended = isset($_POST['recommended']) ? $_POST['recommended'] : false;
        $importance = isset($_POST['importance']) ? $_POST['importance'] : false;
        $nationality = isset($_POST['nationality']) ? $_POST['nationality'] : false;

        $keywords = isset($_POST['keywords']) ? $_POST['keywords'] : false;
        $broker = isset($_POST['broker']) ? $_POST['broker'] : false;
        $home_address = isset($_POST['home_address']) ? $_POST['home_address'] : false;
		
        $mobile = isset($_POST['mobile']) ? $_POST['mobile'] : false;		
        $customer_source = isset($_POST['customer_source']) ? $_POST['customer_source'] : false;
        $category = isset($_POST['category']) ? $_POST['category'] : false;
        $company_name = isset($_POST['company_name']) ? $_POST['company_name'] : false;
        $building_name = isset($_POST['building_name']) ? $_POST['building_name'] : false;
        //$building_name = isset($_POST['building_name']) ? $_POST['building_name'] : false;

        $sctime = isset($_POST['sctime']) ? $_POST['sctime'] : false;
        $ectime = isset($_POST['ectime']) ? $_POST['ectime'] : false;

        $sbirthday = isset($_POST['sbirthday']) ? $_POST['sbirthday'] : false;
        $ebirthday = isset($_POST['ebirthday']) ? $_POST['ebirthday'] : false;

        $send_publication = isset($_POST['send_publication']) ? $_POST['send_publication'] : false;
        $send_email = isset($_POST['send_email']) ? $_POST['send_email'] : false;
        $gifts = isset($_POST['gifts']) ? $_POST['gifts'] : false;

        $where = '';
        $where_flag = TRUE;

        if ($gifts) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }

            $where .= "  `gifts` = '" . $gifts . "' ";
        }

        if ($send_email) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }

            $where .= "  `send_email` = '" . $send_email . "' ";
            $where .= " OR `send_email1` = '" . $send_email . "' ";
            $where .= " OR `send_email2` = '" . $send_email . "' ";
        }

        if ($send_publication) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }

            $where .= "  `home_send_publication` = '" . $send_publication . "' ";
            $where .= " OR `home_send_publication1` = '" . $send_publication . "' ";
            $where .= " OR `home_send_publication2` = '" . $send_publication . "' ";

            $where .= " OR `company_send_publication` = '" . $send_publication . "' ";
            $where .= " OR `company_send_publication1` = '" . $send_publication . "' ";
            $where .= " OR `company_send_publication2` = '" . $send_publication . "' ";
            
        }

        if ($sbirthday OR $ebirthday) {

            $ebirthday = strtotime($ebirthday);
            $sbirthday = strtotime($sbirthday);

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }

            $where .= "  `birthday_int` >= '" . $sbirthday . "' " . " AND `birthday_int` <= '" . $ebirthday . "' ";
        }

        if ($ectime OR $sctime) {

            $ectime = strtotime($ectime);
            $sctime = strtotime($sctime);

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }

            $where .= " ( `ctime` >= '" . ($sctime-(60*24*60)) . "'  AND `ctime` <= '" . ($ectime+(60*24*60) ). "' ) ";
        }

        if ($building_name) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }
            $where .= "  `home_building_name` LIKE '%" . $building_name . "%' ";
            $where .= " OR `home_building_name1` LIKE '%" . $building_name . "%' ";
            $where .= " OR `home_building_name2` LIKE '%" . $building_name . "%' ";
        }

        if ($company_name) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }
            $where .= "  ( `company_name1` LIKE '%" . $company_name . "%' ";
            $where .= " OR `company_name2` LIKE '%" . $company_name . "%' ";
            $where .= " OR `company_name` LIKE '%" . $company_name . "%' ) ";
        }

        if ($category) {

            $_cc  =  explode(",", $category);

            foreach($_cc as $_aa => $vv ){

                if(strlen($vv)>2){
                    if ($where_flag) {
                        $where .= " WHERE ";
                        $where_flag = false;
                    } else {
                        $where .= " OR ";
                    }

                    $where .= "  `category` LIKE '%" . trim($vv) . "%' ";
                    $where .= " OR `category1` LIKE '%" . trim($vv) . "%' ";
                    $where .= " OR `category2` LIKE '%" . trim($vv) . "%' ";
                }
            }

        }

        // $this->load->model("insert_model");

        // if ($customer_source) {

        //   //$this->insert_model->insert("h_sql" , array("SQL"=>$customer_source));
        //     $c_s_s_res = $this->customer_source_seslove($customer_source);

        //     foreach( $c_s_s_res as $_k => $_v){

        //         if ($where_flag) {

        //             $where .= " WHERE ";
        //             $where_flag = false;

        //         } else {

        //             $where .= " OR ";

        //         }

        //         $where .= "  `customer_source` = '" . $_k . "' ";
        //     }


        // }


        if ($mobile) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }

            $where .= "  ( `mobile` LIKE '%" . $mobile . "%' ";
            $where .= " OR  `mobile1` LIKE '%" . $mobile . "%' ";
            $where .= " OR  `mobile2` LIKE '%" . $mobile . "%' ";

            $where .= " OR  `home_phone` LIKE '%" . $mobile . "%' ";
            $where .= " OR  `home_phone1` LIKE '%" . $mobile . "%' ";
            $where .= " OR  `home_phone2` LIKE '%" . $mobile . "%' ";

            $where .= " OR  `company_phone` LIKE '%" . $mobile . "%' ";
            $where .= " OR  `company_phone1` LIKE '%" . $mobile . "%' ";
            $where .= " OR  `company_phone2` LIKE '%" . $mobile . "%' )";

        }

        if ($home_address) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " OR ";
            }
            $where .= "  `home_address` LIKE '%" . $home_address . "%' ";
        }

        // if ($keywords) {

        //     if ($where_flag) {
        //         $where .= " WHERE ";
        //         $where_flag = false;
        //     } else {
        //         $where .= " OR ";
        //     }
        //     $where .= "     `background_and_features` LIKE '%" . $keywords . "%' ";

        //     $where .= "  OR `email` LIKE '%" . $keywords . "%' ";
        //     $where .= "  OR `email1` LIKE '%" . $keywords . "%' ";
        //     $where .= "  OR `email2` LIKE '%" . $keywords . "%' ";

        //     //$c_s_s_res = $this->customer_source_seslove($keywords);
        //     //去找看看有有一樣的客戶來源
        //     foreach( $c_s_s_res as $_k => $_v){

        //         if ($where_flag) {

        //             $where .= " WHERE ";
        //             $where_flag = false;

        //         } else {

        //             $where .= " OR ";

        //         }

        //         $where .= "  `customer_source` LIKE '%" . $_k . "%' ";
        //     }

        // }

        if ($broker) {

        	//$this->load->model("users_model");
			//$users_data=$this->users_model->get_user_s_name($broker);


			//foreach($users_data->result() as $user ){

	            if ($where_flag) {
	                $where .= " WHERE ";
	                $where_flag = false;
	            } else {
	                $where .= " AND ";
	            }


	            $where .= "  `broker` = '" . $broker . "' ";
			//}
        }

        if ($name) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }
            $where .= "  `name` LIKE '%" . $name . "%' ";
        }

        if ($recommended) {

            if ($where_flag) {
                $where .= " WHERE ";
                $where_flag = false;
            } else {
                $where .= " AND ";
            }
            $where .= "  `recommended` LIKE '%" . $recommended . "%' ";
        }

        if ($importance) {

            //$this -> circle_model -> insert_sql_where($str);

            $importance_array = explode(',', $importance);

            foreach ($importance_array as $key => $str) {

                //$this -> circle_model -> insert_sql_where($str);

                if (strlen($str)>2) {

                    if ($where_flag) {
                        $where .= " WHERE ";
                        $where_flag = false;
                    } else {
                        $where .= " OR ";
                    }

                    $where .= "  `importance` = '" . trim($str) . "' ";
                }
            }
        }

        if ($nationality){

            $stuednt_array = explode(",", $nationality);

            $flgg = true;


            foreach ($stuednt_array as $index => $value) {

                if (!empty($value)) {

                    if ($where_flag) {

                        $where .= " WHERE ";
                        $where_flag = false;

                        if($flgg ){

                            $where.=' ( ';
                            $flgg=false;
                        }

                    } else {

                        //$where .= " OR ";

                        if($flgg ){

                            $where .= " OR (";
                            $flgg=false;

                        }else{

                            $where .= " OR ";

                        }

                    }



                    $where .= "  `nationality` LIKE '%" . $value . "%' ";
                }
                 //$where.=" ) ";
            }

            $where.=" ) ";
        }

        //經紀人是否可見
        // $CI = &get_instance();
        // $purview = $CI->login_lib->purview();
        // $users_id = $CI->login_lib->users_id();

        // $this->load->library('show_test');
        // if (!$this->show_test->is_broker_visible()) {

        //     if ($where_flag) {
        //         $where .= " WHERE ";
        //         $where_flag = false;
        //     } else {
        //         $where .= " AND ";
        //     }

        //     $where .= "  `broker` = '" . $users_id . "' ";
        // }

       return $where;
	//echo 'kakichung';		
	}

}
